package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.reactive.TransactionalOperator;

/**
 * 护理计划Service业务层处理
 * 
 * @author zzyl
 * @date 2025-02-20
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划vo
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //1.获取护理计划基本信息
        NursingPlan nursingPlan = getById(id);
        //2.护理计划项目相关信息
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(
                new LambdaQueryWrapper<NursingProjectPlan>()
                        .eq(NursingProjectPlan::getPlanId,id));
        //拷贝集合，用的是hutool的方法
        List<NursingProjectPlanVo> projectPlanVos = BeanUtil.copyToList(nursingProjectPlans, NursingProjectPlanVo.class);
        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        vo.setProjectPlans(projectPlanVos);
        return vo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划项目Dto
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.保存护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        //对象拷贝
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        boolean flag = save(nursingPlan);
        if(!flag){
            throw new BaseException("保存护理计划基本信息失败");
        }
        //2.保存护理计划项目关联信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(v->v.setPlanId(nursingPlan.getId()));
        flag = nursingProjectPlanService.saveBatch(projectPlans);
        if(!flag){
            throw new BaseException("增加护理计划相关的护理项目信息失败");
        }
        return 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划DTO
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        boolean flag = updateById(nursingPlan);
        if(!flag){
            throw new BaseException("修改护理计划基本信息失败");
        }
        //删除护理计划相关的护理项目信息
        flag = nursingProjectPlanService.remove(
                new LambdaQueryWrapper<NursingProjectPlan>()
                        .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
        if(!flag){
            throw new BaseException("删除护理计划相关的护理项目信息失败");
        }
        //增加护理计划相关的护理项目信息
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(v->v.setPlanId(nursingPlan.getId()));
        flag = nursingProjectPlanService.saveBatch(projectPlans);
        if(!flag){
            throw new BaseException("增加护理计划相关的护理项目信息失败");
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids)
    {
        // return nursingPlanMapper.deleteNursingPlanByIds(ids);
        return removeBatchByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        // return nursingPlanMapper.deleteNursingPlanById(id);
        return removeById(id) ? 1 : 0;
    }
}
